package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.ScoreItemJdbc;
import com.wuseguang.springcloud.dao.mapper.ScoreItemMapper;
import com.wuseguang.springcloud.dao.repository.*;
import com.wuseguang.springcloud.entity.EvaluationLabelAndScoreItem;
import com.wuseguang.springcloud.entity.EvaluationModelItem;
import com.wuseguang.springcloud.entity.ScoreItem;
import com.wuseguang.springcloud.entity.TeacherClassEvaluationItems;
import com.wuseguang.springcloud.service.ScoreItemService;
import com.wuseguang.springcloud.util.Result;
import com.wuseguang.springcloud.util.UpdateTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 评分项表 服务实现类
 * </p>
 *
 * @author
 * @since 2020-02-02
 */
@Service
public class ScoreItemServiceImpl implements ScoreItemService {


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ScoreItemRepository scoreItemRepository;
    @Autowired
    private ScoreItemMapper scoreItemMapper;
    @Autowired
    private ScoreItemJdbc scoreItemJdbc;
    @Autowired
    private EvaluationLabelAndScoreItemRepository evaluationLabelAndScoreItemRepository;
    @Autowired
    private TeacherClassEvaluationItemsRepository teacherClassEvaluationItemsRepository;
    @Autowired
    private EvaluationModelItemRepository evaluationModelItemRepositorye;

    @Override
    public Result findScoreItem(String teacherId) {
        logger.info("查询教师有的评分项 服务实现类");

        String sql = "select si.id,si.name,si.fraction,si.icon ,si.type,si.teacher_id teacherId ,GROUP_CONCAT(el.evaluation_model_id) evaluationTypeId from score_item si LEFT JOIN evaluation_model_item el ON ( el.evaluation_item_id = si.id )   where si.teacher_id= ?  GROUP BY si.id";

        List<Map<String, Object>> itemList = scoreItemJdbc.queryListMap(sql, teacherId);

        return new Result(itemList);
    }

    @Override
    public Result addScoreItem(ScoreItem scoreItem, String teacherId) {

        logger.info("添加教师的评分项");

        //添加的所有教师
        Integer[] teacherIds = scoreItem.getTeacherIds();

        if (!StringUtils.isEmpty(teacherIds)) {

            //去重 防止重复添加
            Set<Integer> set = new HashSet<>();
            for (int i = 0; i < teacherIds.length; i++) {
                set.add(teacherIds[i]);
            }


            //循环每个教师的数据
            for (Integer str : set) {
                scoreItem.setId(0);
                //循环添加评价项信息
                scoreItem.setReleaseTeacherId(Integer.valueOf(teacherId));
                scoreItem.setTeacherId(str);
                Integer id = scoreItemRepository.save(scoreItem).getId();
                //获取这个评价项都的标签集合
                Integer[] tagIdList = scoreItem.getTagId();
                //循环标签id
                for (int j = 0; j < tagIdList.length; j++) {
                    EvaluationModelItem model = new EvaluationModelItem();
                    model.setEvaluationModelId(tagIdList[j]);
                    model.setEvaluationItemId(id);
                    evaluationModelItemRepositorye.save(model);
                }

            }
        } else {
            scoreItem.setReleaseTeacherId(Integer.valueOf(teacherId));
            scoreItem.setTeacherId(Integer.valueOf(teacherId));
            Integer id = scoreItemRepository.save(scoreItem).getId();
            //获取这个评价项都的标签集合
            Integer[] tagIdList = scoreItem.getTagId();
            //循环标签id
            for (int j = 0; j < tagIdList.length; j++) {
                EvaluationModelItem model = new EvaluationModelItem();
                model.setEvaluationModelId(tagIdList[j]);
                model.setEvaluationItemId(id);
                evaluationModelItemRepositorye.save(model);
            }
        }

        return new

                Result();

    }

    @Override
    public Result deleteScoreItem(Integer id) {
        logger.info("删除评分项");
        //先删除关联
        String sql = "DELETE FROM `evaluation_label_and_score_item` WHERE `score_item_id` = ?";
        scoreItemJdbc.updateByParameter(sql, id);

        //删除数据
        scoreItemRepository.deleteById(id);

        return new Result();
    }

    @Override
    public Result updateScoreItem(ScoreItem scoreItem) {


        //先删除关联
        String sql = "DELETE FROM `evaluation_model_item` WHERE `evaluation_item_id` = ?";
        scoreItemJdbc.updateByParameter(sql, scoreItem.getId());

        //查询原线的信息
        ScoreItem scoreItemTow = scoreItemRepository.findById(scoreItem.getId()).get();
        UpdateTool.copyNullProperties(scoreItemTow, scoreItem);

        scoreItemRepository.save(scoreItem);
        //获取这个评价项都的标签集合
        Integer[] tagIdList = scoreItem.getTagId();
        //循环标签id
        for (int i = 0; i < tagIdList.length; i++) {
            EvaluationModelItem model = new EvaluationModelItem();
            model.setEvaluationModelId(tagIdList[i]);
            model.setEvaluationItemId(scoreItem.getId());
            evaluationModelItemRepositorye.save(model);
        }
        return new Result();
    }

    @Override
    public Result importClassEvaluationItems(Integer classId, Integer[] scoreItemId, String teacherId, Integer type) {

        //先删除这个老师在这个班级有的评分项
        try {
            int ss = teacherClassEvaluationItemsRepository.deleteByClassIdAndTecherId(classId, teacherId, type);
            System.err.println(ss);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //循环评分项id
        for (int i = 0; i < scoreItemId.length; i++) {
            TeacherClassEvaluationItems t = new TeacherClassEvaluationItems();
            t.setClassId(classId);
            t.setScoreItemId(scoreItemId[i]);
            t.setTeacherId(Integer.valueOf(teacherId));
            t.setType(type);
            teacherClassEvaluationItemsRepository.save(t);
        }

        return new Result();
    }

    @Override
    public Result findScoreItemType(Integer classId, Integer type, String teacherId) {
        //根据类型查询这个老师在这个班级的评价项状态
        String sql = "select si.id,si.name,si.fraction,si.icon,if(IFNULL(tcei.id,0)=0,0,1) state,tcei.id tceiId from score_item si left join " +
                " teacher_class_evaluation_items tcei on(si.id=tcei.score_item_id and tcei.class_id =?) where si.teacher_id= ? and si.type =? ";
        //查询结果
        List<Map<String, Object>> list = scoreItemJdbc.queryListMap(sql, classId, teacherId, type);

        return new Result(list);
    }

    @Override
    public Result findTecherClassScoreItem(Integer classId, String teacherId) {
        //查询这个老师在这个班的评分项
        String sql = "select si.id,si.name,si.fraction,si.icon ,si.type ," +
                "si.teacher_id teacherId ,GROUP_CONCAT(el.evaluation_model_id) evaluationTypeId ," +
                "tcei.id tceiId from score_item si left join  teacher_class_evaluation_items tcei " +
                "on(si.id=tcei.score_item_id ) LEFT JOIN evaluation_model_item el " +
                "ON ( el.evaluation_item_id = si.id ) where si.teacher_id= ? " +
                "and tcei.class_id =?  GROUP BY si.id";
        //查询结果
        List<Map<String, Object>> list = scoreItemJdbc.queryListMap(sql, teacherId, classId);

        return new Result(list);
    }

    @Override
    public Result delteTecherClassScoreItem(Integer id) {

        teacherClassEvaluationItemsRepository.deleteById(id);
        return new Result();
    }
}
